#!/bin/bash
#-------------------------------------------------------------------------------------------------------------
#该脚本的使用方式为-->[sh startup.sh]
#该脚本可在服务器上的任意目录下执行,不会影响到日志的输出位置等
#-------------------------------------------------------------------------------------------------------------
#颜色设置
#SETCOLOR_SUCCESS="echo -en \\033[1;32m"
#SETCOLOR_FAILURE="echo -en \\033[1;31m"
#SETCOLOR_NORMAL="echo -en \\033[0;39m"

#设定环境变量

source /etc/profile

APP_NAME=sw-lmsc
LOG_FILE_NAME=nohup.log
PROFILE=prod

JAR_ZIP_DIR=/opt/qypay/${APP_NAME}

APP_LOG_FILE=$JAR_ZIP_DIR/${LOG_FILE_NAME}
JAR_FILE=`cd ${JAR_ZIP_DIR} && ls -t ${APP_NAME}*.jar |head -n 1`

# 今日日期：yyMmdd
TODAY=$(date -d "now" +"%Y%m%d")

JAVA_OPTS=" -Xloggc:/opt/logs/${APP_NAME}_gc.log -server -Xmx512M -Xms512M -Xmn256M -XX:MaxMetaspaceSize=128M -XX:MetaspaceSize=128M -XX:+UseConcMarkSweepGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"

####获取pid ###
tradePortalPID=0
getTradeProtalPID(){
    javaps=`ps -ef | grep $APP_NAME |grep java |grep -v grep`
    if [ -n "$javaps" ]; then
        tradePortalPID=`ps -ef | grep ${APP_NAME} |grep java |grep -v grep| awk '{print $2}'`
    else
        tradePortalPID=0
    fi
}

#### 备份系统日志 ####
backupSysLog() {
	echo "Starting backup app sys log ........"

	if [ -f $APP_LOG_FILE ];then

		# 日志备份目录中，今日备份日志的数量
		TODAY_BACKUP_COUNT=`ll | grep -c $TODAY`
		cp $APP_LOG_FILE $TODAY-$TODAY_BACKUP_COUNT.log

		echo -e "\tBackup sys log  .............                    \033[32;49;1m [SUCCESS] \033[39;49;0m"
	else
		echo -e "\tSys log file doesn\'t exist, so no need backup  ............."
	fi

}


#### 启动 ######

start(){
    getTradeProtalPID
#    echo "================================================================================================================"
    if [ ${tradePortalPID} -ne 0 ]; then
        echo "${APP_NAME} already started(PID=${tradePortalPID})"
#        echo "================================================================================================================"
    else
        backupSysLog

        echo  "Starting $APP_NAME ........"
        nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $JAR_ZIP_DIR/$JAR_FILE --spring.profiles.active=${PROFILE}  > $JAR_ZIP_DIR/${LOG_FILE_NAME} &
        sleep 2
        getTradeProtalPID
        if [ $tradePortalPID -ne 0 ]; then
            echo -e THE ${APP_NAME}\(PID=${tradePortalPID}\)  ............."                    \033[32;49;1m [SUCCESS] \033[39;49;0m"
#            echo  "================================================================================================================"
        else
            echo -e THE ${APP_NAME}\(PID=${tradePortalPID}\)  ............."                     \033[31;49;1m [Failed] \033[39;49;0m"
#            echo  "================================================================================================================"
        fi
    fi
}


##### 关闭 ########
stop() {
#        echo "================================================================================================================"
        echo "Stopping $APP_NAME ........"
        getTradeProtalPID
        if [ $tradePortalPID -ne 0 ]; then
                /bin/kill -9 $tradePortalPID & getTradeProtalPID
#                killall phantomjs
                sleep 2
                echo -e THE ${APP_NAME}\(PID=${tradePortalPID}\) ............."                    \033[32;49;1m [SUCCESS] \033[39;49;0m"

#               echo "================================================================================================================"
        else
                sleep 2
                echo -e THE ${APP_NAME}\(PID=${tradePortalPID}\) ............."                     \033[31;49;1m [Failed] \033[39;49;0m"
#               echo "================================================================================================================"

        fi

}

#### 重启
restart() {
    stop
    sleep 2
    start
}


#### 选项
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 2
esac
